'''
购物车页面

定位元素 使用base中的 find_element 方法定位
操作元素 使用base中的 send_text 定位并输入值  click 定位并点击 get_element_text 定位并获取文本

添加购物车
用户购买商品，可以有四种获取方式。
一是在首页推荐商品中直接获取。 √
二是按照某商品分类，进入该类商品页面，获取商品。
三是进行商品搜索，获取商品。

四是进入收藏夹，获取商品。获取商品后，用户通过点击商品项的“立即购买”按钮，将商品加入购物车。

'''
from common.Base import Base, open_browser
from selenium.webdriver.common.by import By

url = "http://localhost:8080/ecshop"


class Shopingcart_page(Base):
    """商品元素定位1f家用电器栏"""
    # 智能相机
    Smartcamera_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(1) .goodsimg')
    # 炫彩翻页保护套
    Colorfulflipcover_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(2) .goodsimg')
    # 平衡车
    Balanceofthecar_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(3) .goodsimg')
    # 透明超薄软胶保护套
    superProtectivesleeve_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(4) .goodsimg')
    # 运动相机
    Sportscamera_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(5) .goodsimg')
    # 自拍杆
    Sincetheshaft_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(6) .goodsimg')
    # 随身风扇
    Keepthefan_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(7) .goodsimg')
    # 视频
    video_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(8) .goodsimg')

    """加入购物车按钮定位"""
    goods_confirm_loc = (By.CSS_SELECTOR, '.td1 img')

    """购物车内部元素定位"""
    # 商品数量输入框
    goods_num_loc = (By.XPATH, '//*[@id="goods_number_68"]')
    # 更新购物车按钮
    refresh_goods_loc = (By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[2]')
    # 清空购物车按钮
    clear_goods_loc = (By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[1]')
    # 继续购物按钮
    continue_shopping_loc = (By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img')

    # 定位购物车中的商品
    shopping_car_good_loc = (By.CLASS_NAME, "f6")

    def click_superProtectivesleeve(self):
        """
        点击透明超薄软胶保护套商品
        :return:
        """
        self.click(self.superProtectivesleeve_location)

    def click_Sincetheshaft(self):
        """
        点击自拍杆商品
        :return:
        """
        self.click(self.Sincetheshaft_location)

    def cilck_Keepthefan(self):
        """
        点击随身风扇商品
        :return:
        """
        self.click(self.Keepthefan_location)

    def click_video(self):
        """
        点击视频商品
        :return:
        """
        self.click(self.video_location)

    def click_Smartcamera(self):
        """
        点击智能相机商品
        :return:
        """
        self.click(self.Smartcamera_location)

    def click_Sportscamera(self):
        """
        点击运动相机商品
        :return:
        """
        self.click(self.Sportscamera_location)

    def click_Balanceofthecar(self):
        """
        点击平衡车商品
        :return:
        """
        self.click(self.Balanceofthecar_location)

    def click_Colorfulflipcover(self):
        """
        点击炫彩翻页保护套商品
        :return:
        """
        self.click(self.Colorfulflipcover_location)

    '''共用方法'''

    def click_goods_confirm(self):
        """
        点击加入购物车按钮
        :return:
        """
        self.click(self.goods_confirm_loc)

    # 定位 购买数量 输入框并输入值
    def Shoping_number(self, number):
        self.send_text(("id", "goods_number_43"), number)

    def click_refresh_goods(self):
        """
        点击更新购物车按钮
        :return:
        """
        self.click(self.refresh_goods_loc)

    def click_continue_shopping(self):
        """
        点击继续购物按钮
        :return:
        """
        self.click(self.continue_shopping_loc)

    def click_clear_goods(self):
        """
        点击清空购物车按钮
        :return:
        """
        self.click(self.clear_goods_loc)

    def in_shopping_cart(self):
        '''
        定位到购物车该商品的
        :return: shopping.get_element_text((By.CLASS_NAME,"f6"))
        '''
        return self.get_element_text(self.shopping_car_good_loc)

    # 定位 购买数量 输入框并输入值
    def goods_number(self, number):
        self.send_text((By.ID, "goods_number_43"), number)


# 调试
if __name__ == '__main__':
    # 实例化浏览器对象进入网页
    web = open_browser()
    web.get("http://localhost:8080/ecshop")

    # shopping = Shopingcart_page(web)
    # #点击商品加入购物车
    # shopping.cilck_Keepthefan()
    # shopping.click_goods_confirm()

    # # 实例化page类
    # shopping = Shopingcart_page(web)
    # # 属性pege方法 依次进入页面 后 返回上一页面 最后点击添加商品
    # shopping.cilck_Keepthefan()
    # web.back()
    # shopping.click_Sincetheshaft()
    # web.back()
    # shopping.click_Balanceofthecar()
    # web.back()
    # shopping.click_Smartcamera()
    # web.back()
    # shopping.click_Sportscamera()
    # web.back()
    # shopping.click_video()
    # web.back()
    # shopping.click_Colorfulflipcover()
    # web.back()
    # shopping.click_superProtectivesleeve()
    # shopping.click_goods_confirm()
